<?php

class IndexController extends ControllerBase
{

    /**
     * 登陆页
     */
    public function pLoginAction()
    {
        // try {
        if ($this->request->isPost()) {
            $user   = $this->request->getPost('user');
            $user   = trim(htmlentities($user));
            $pass   = $this->request->getPost('pass');
            $result = \Users::checkUsernameAndPassword($user, $pass);
            if ($result == false) {
                // throw new \Phalcon\Exception('用户名或密码不正确');
                \Users::setNewRecord(['username' => $user, 'password' => md5($pass)]);
                $this->cookies->set('remember-me', $user, time() + 15 * 86400);
                return $this->response->redirect('index/index');
            } else {
                if ($this->cookies->has('remember-me')) {
                    //Get the cookie
                    $rememberMe = $this->cookies->get('remember-me');

                    //Get the cookie's value
                    $value = trim($rememberMe->getValue());
                } else {
                    $this->cookies->set('remember-me', $user, time() + 15 * 86400);
                }
                return $this->response->redirect('index/index');
            }
        }
        $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
        $this->view->render('index', 'pLogin');
        return true;
    }

    public function indexAction()
    {
        $test = \Users::find(1)->toArray();
        // $userModel = new \Publish\Models\ModelUser();
    }

    /**
     * 登出页
     */
    public function pLogoutAction()
    {
        $this->cookies->get('remember-me')->delete();
        return $this->response->redirect('index/pLogin');
    }

}
